/**
 * 
 */
package com.naiterui.ehp.bs.patient.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.naiterui.ehp.bp.utils.notice.SendMessageUtil;
import com.naiterui.ehp.bp.utils.thread.ThreadPoolUtil;

public class SMSUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(SMSUtil.class);

    private static SendMessageUtil sendMessageUtil;

    /**
     * 初始化短信接口
     */
    static {
        String url = ConfigKeyUtil.getString("api.sms.singlesned.url");
        sendMessageUtil = new SendMessageUtil(url);
    }

    public enum Template {
        /** 获取验证码短信模板 */
        VERIFICATION_CODE_TEMPLATE (ConfigKeyUtil.getString("verification.code.template")),
        /** 取消视频预约患者通知短信模板 */
        RESERVATION_PATIENT_CANCEL_NOTICE_TEMPLATE (ConfigKeyUtil.getString("reservation.patient.cancel.notice.template")),
        /** 取消视频预约医生通知短信模板 */
        RESERVATION_DOCTOR_CANCEL_NOTICE_TEMPLATE (ConfigKeyUtil.getString("reservation.doctor.cancel.notice.template")),
        /** 视频预约成功患者通知短信模板 */
        RESERVATION_PATIENT_SUCCESS_NOTICE_TEMPLATE (ConfigKeyUtil.getString("reservation.patient.success.notice.template")),
        /** 视频预约成功医生通知短信模板 */
        RESERVATION_DOCTOR_SUCCESS_NOTICE_TEMPLATE (ConfigKeyUtil.getString("reservation.doctor.success.notice.template")),
        /** 图文咨询付费成功通知 */
        SMS_MESSAGE_CONSULT_POINT_NOTICE_TEMPLATE (ConfigKeyUtil.getString("consult.point.notice.template")),
        /** 个性化服务付费成功通知 */
        CUSTOM_CHARGE_SMS_TEMPLATE (ConfigKeyUtil.getString("custom.charge.sms.template")),
        /** 患者给医生打赏,医生获取积分短信 */
        SMS_REWARD_POINT_NOTICE_TEMPLATE (ConfigKeyUtil.getString("sms.reward.point.notice.template"));

        private String templateId;

        Template(String templateId) {
            this.templateId = templateId;
        }

        public String getTemplateId() {
            return this.templateId;
        }

        public void setTemplateId(String templateId) {
            this.templateId = templateId;
        }

    }

    /**
     * <根据模板发送短信>
     * @param phone
     * @param tmpl
     * @param args void <返回值描述>
     * @Throws 异常信息
     * @History 2015-8-21 下午2:30:02 by guoyongxiang
     */
    public static void sendSMS(String phone, Template tmpl, Object... args) {
        String templateId = tmpl.getTemplateId();
        try {
            if (StringUtils.isNotEmpty(templateId)) {
                sendMessageUtil.sendSms(phone, templateId, args);
                LOGGER.info("[smsSend]短信发送phone: {}, templateId: {}, params: {}", phone, templateId, args);
            } else {
                LOGGER.warn("[smsSend]短信模板内容为空phone: {}, templateId: {}, params: {}", phone, templateId, args);
            }
        } catch (Exception e) {
            LOGGER.warn("[smsSend]短信模板内容为空phone: {}, templateId: {}, params: {}", phone, templateId, args);
        }
    }

    /**
     * 异步短信发送方法
     * SMSUtil.asyncSendSMS()

     * @Date 2017年10月31日
     * @since 2.9.2
     * @param phone
     * @param tmpl
     * @param args
     */
    public static void asyncSendSMS(final String phone, final Template tmpl, final Object... args) {
        ThreadPoolUtil.execute(new Runnable() {
            @Override
            public void run() {
                sendSMS(phone, tmpl, args);
            }
        });
    }
}
